﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Diagnostics;
using Guids;

namespace GuidGeneratorWPF
{
    public class GuidFormatToBooleanConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is GuidFormats))
                throw new InvalidOperationException("value must be a GuidFormats");

            if (parameter == null)
                throw new InvalidOperationException("parameter must not be null");

            GuidFormats guidFormat = (GuidFormats)value;
            
            GuidFormats param;

            if (!Enum.TryParse<GuidFormats>(parameter.ToString(), out param))
                throw new InvalidOperationException("parameter must parse to a GuidFormats");

            return guidFormat == param;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is bool))
                throw new InvalidOperationException("value must be a bool");

            if ((bool)value)
            {
                GuidFormats param;

                if (!Enum.TryParse<GuidFormats>(parameter.ToString(), out param))
                    throw new InvalidOperationException("parameter must parse to a GuidFormats");

                Debug.WriteLine("Converted value {0} and parameter {1} to {2}", value, parameter, param);

                return param;
            }

            return null;
        }

        #endregion
    }
}
